/**   
 * @Title: LotteryInfoExpendsAdapter.java 
 * @Package: com.esun.rabbit2.viewer.mainact.lotteryinfoact 
 * @Description: 可展开List适配器 
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-8-24 下午02:36:42  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryinfoact;

import java.util.ArrayList;
import java.util.Hashtable;

import com.esun.config.Config;
import com.esun.ui.R;
import com.esun.util.img.ImageListCache;
import com.esun.util.log.EDebug;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @ClassName: LotteryInfoExpendsAdapter
 * @Description: 可展开List适配器
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-8-24 下午02:36:42
 */
public class LotteryInfoExpendsAdapter extends BaseExpandableListAdapter {

	private ArrayList<Hashtable<String, String>> group = new ArrayList<Hashtable<String, String>>();
	private ArrayList<ArrayList<Hashtable<String, String>>> child = new ArrayList<ArrayList<Hashtable<String, String>>>();

	private ArrayList<View> groupView = new ArrayList<View>();
	private ArrayList<ArrayList<View>> childView = new ArrayList<ArrayList<View>>();

	private LayoutInflater listInflater;

	public ImageListCache cacheUtil;

	private Context context;

	private String lotid;

	public LotteryInfoExpendsAdapter(Context context, ArrayList<Hashtable<String, String>> groupContent, ArrayList<View> groupContentView,
			ArrayList<ArrayList<Hashtable<String, String>>> childContent, ArrayList<ArrayList<View>> childContentView, String lotid) {

		this.group = groupContent;
		this.child = childContent;
		this.groupView = groupContentView;
		this.childView = childContentView;
		this.listInflater = LayoutInflater.from(context);
		this.context = context;
		this.lotid = lotid;
		intHandler();

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return child.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

		if (childView.get(groupPosition).size() < childPosition + 1) {

			convertView = (RelativeLayout) listInflater.inflate(R.layout.lotteryinfo_list_table_football, null);
			ImageView imgHome;	
			ImageView imgVisit;
			TextView homeName;
			TextView visitingName;
			TextView order = (TextView) convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_order);
			TextView leaguename = (TextView) convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_leagueName);
			TextView score = (TextView) convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_score);
			TextView result =(TextView) convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_result);
			ImageView imgResult = (ImageView) convertView.findViewById(R.id.lotteryinfo_listtable_football_img_result);
			
			Hashtable <String, String> htInfo = new Hashtable <String, String> ();
			htInfo = child.get(groupPosition).get(childPosition);
			String matchnum = htInfo.get("matchnum");
			String hometeam = htInfo.get("hometeam");
			String visitingteam = htInfo.get("visitingteam");
			String leagueName = htInfo.get("leaguename");
			String strScore = htInfo.get("score");
			String strResult = htInfo.get("result");
			
			
			if (lotid.equals(Config.JINGCAILANQIU)) {
				imgHome = (ImageView) convertView.findViewById(R.id.lotteryinfo_listtable_football_image_visitingTeam);
				imgVisit = (ImageView) convertView.findViewById(R.id.lotteryinfo_listtable_football_image_homeTeam);
				homeName = (TextView)convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_visitingName);
				visitingName = (TextView)convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_homeName);
				if (strResult.indexOf("胜") > 0) {
					imgResult.setBackgroundResource(R.drawable.icon_default_information_win_basket_80x60);
				} else if (strResult.indexOf("负") > 0) {
					imgResult.setBackgroundResource(R.drawable.icon_default_information_lose_basket_80x60);
				}
			} else {
				imgHome = (ImageView) convertView.findViewById(R.id.lotteryinfo_listtable_football_image_homeTeam);
				imgVisit = (ImageView) convertView.findViewById(R.id.lotteryinfo_listtable_football_image_visitingTeam);
				homeName = (TextView)convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_homeName);
				visitingName = (TextView)convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_visitingName);
				TextView rangqiu = (TextView) convertView.findViewById(R.id.lotteryinfo_listtable_football_tv_rangqiu);
				String rangQiu = htInfo.get("rangqiu");
				rangqiu.setText(rangQiu);
				if (strResult.indexOf("胜") > 0) {
					imgResult.setBackgroundResource(R.drawable.icon_default_information_win_80x60);
				} else if (strResult.indexOf("平") > 0) {
					imgResult.setBackgroundResource(R.drawable.icon_default_information_tie_80x60);
				} else if (strResult.indexOf("负") > 0) {
					imgResult.setBackgroundResource(R.drawable.icon_default_information_lose_80x60);
				}
			}
			order.setText(matchnum);
			leaguename.setText(leagueName);
			score.setText(strScore);
			result.setText(strResult);
			homeName.setText(hometeam);
			visitingName.setText(visitingteam);
			

			childView.get(groupPosition).add(convertView);

			Object[] objs = { child.get(groupPosition).get(childPosition).get("hometeam_img"), imgHome };

			Object[] obj = { child.get(groupPosition).get(childPosition).get("visit_img"), imgVisit };

			ArrayList<Object[]> alSending = new ArrayList<Object[]>();
			alSending.add(obj);
			alSending.add(objs);

			msg = handler.obtainMessage(ADD_NEW_IMGNAME, alSending);
			handler.sendMessage(msg);

			EDebug.o(LotteryInfoExpendsAdapter.this, "创建新的子view  groupPosition【" + groupPosition + "】   childPosition【" + childPosition + "】");

			return convertView;
		} else {
			return childView.get(groupPosition).get(childPosition);
		}
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return child.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		EDebug.o(this, groupPosition);
		if (groupView.size() < groupPosition + 1) {
			convertView = (LinearLayout) listInflater.inflate(R.layout.lotteryinfo_football_listtable_title, null);
			RelativeLayout relative = (RelativeLayout) convertView.findViewById(R.id.lotteryinfo_football_listtable_relative_expand);
			TextView tvdate = (TextView) convertView.findViewById(R.id.lotteryinfo_listtitle_football_tv_date);
			TextView tvweek = (TextView) convertView.findViewById(R.id.lotteryinfo_listtitle_football_tv_week);
			TextView tvday = (TextView) convertView.findViewById(R.id.lotteryinfo_listtitle_football_tv_count);
			// kael添加：根据彩种判断日期显示条的颜色
			if (lotid.equals(Config.JINGCAIZUQIU)) {// 竞彩足球 绿色
				relative.setBackgroundResource(R.drawable.btn_kjxx_group_style);
				tvdate.setTextColor(context.getResources().getColor(R.color.white));
				tvweek.setTextColor(context.getResources().getColor(R.color.white));
				tvday.setTextColor(context.getResources().getColor(R.color.white));
			} else if (lotid.equals(Config.BEIJINGZUQIUDANCHANG)) {// 北单 绿色
				relative.setBackgroundResource(R.drawable.btn_kjxx_group_style);
				tvdate.setTextColor(context.getResources().getColor(R.color.white));
				tvweek.setTextColor(context.getResources().getColor(R.color.white));
				tvday.setTextColor(context.getResources().getColor(R.color.white));
			} else if (lotid.equals(Config.JINGCAILANQIU)) {// 竞彩篮球 银色
				relative.setBackgroundResource(R.drawable.btn_kjxx_basketball_style);
				tvdate.setTextColor(context.getResources().getColor(R.color.solid_black));
				tvweek.setTextColor(context.getResources().getColor(R.color.solid_black));
				tvday.setTextColor(context.getResources().getColor(R.color.solid_black));
			}

			if (isExpanded) {
				((ImageView) convertView.findViewById(R.id.lotteryinfo_football_listtable_title_btn_expand)).setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.button_default_pickteam_on_38x38));
			} else {
				((ImageView) convertView.findViewById(R.id.lotteryinfo_football_listtable_title_btn_expand)).setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.button_default_pickteam_off_38x38));
			}

			tvdate.setText(group.get(groupPosition).get("date"));
			tvweek.setText(group.get(groupPosition).get("week"));
			tvday.setText(child.get(groupPosition).size() + "场比赛已截止");
			groupView.add(convertView);

			return convertView;
		} else {

			if (isExpanded) {
				((ImageView) groupView.get(groupPosition).findViewById(R.id.lotteryinfo_football_listtable_title_btn_expand)).setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.button_default_pickteam_on_38x38));
			} else {
				((ImageView) groupView.get(groupPosition).findViewById(R.id.lotteryinfo_football_listtable_title_btn_expand)).setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.button_default_pickteam_off_38x38));
			}

			return groupView.get(groupPosition);
		}
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.BaseExpandableListAdapter#onGroupCollapsed(int)
	 */
	@Override
	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.BaseExpandableListAdapter#onGroupExpanded(int)
	 */
	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	private Handler handler;
	private Message msg = new Message();
	private static final int ADD_NEW_IMGNAME = 11111;

	private void intHandler() {

		handler = new Handler() {

			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case ADD_NEW_IMGNAME:

					if (cacheUtil == null) {
						cacheUtil = new ImageListCache(context, (ArrayList<Object[]>) msg.obj);
					} else {
						cacheUtil.addToLine((ArrayList<Object[]>) msg.obj);
					}

					break;

				default:

					break;
				}
			}
		};
	}

	public void stopImageThread() {
		if (cacheUtil != null) {
			cacheUtil.stop();
		}
	}

	public void cleanImageData() {
		if (cacheUtil != null) {
			cacheUtil.cleanDatas();
		}
	}

	public boolean isCacheUtilNull() {
		if (cacheUtil == null) {
			return true;
		} else {
			return false;
		}
	}

}
